<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>星座运势 AI 助手</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Helvetica Neue', Arial, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
            padding: 20px;
        }
        
        .container {
            display: flex;
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
            overflow: hidden;
            height: 80vh;
        }
        
        .sidebar {
            width: 300px;
            background: linear-gradient(180deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            padding: 30px 20px;
            overflow-y: auto;
        }
        
        .sidebar-header {
            text-align: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .sidebar-header h1 {
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .zodiac-list {
            display: grid;
            grid-template-columns: 1fr;
            gap: 15px;
        }
        
        .zodiac-item {
            background: rgba(255, 255, 255, 0.1);
            padding: 15px;
            border-radius: 10px;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            font-weight: 500;
        }
        
        .zodiac-item:hover {
            background: rgba(255, 255, 255, 0.2);
            transform: translateX(5px);
        }
        
        .chat-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            padding: 30px;
            background-color: #f9f9f9;
        }
        
        .chat-header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid #eee;
        }
        
        .chat-header h2 {
            font-size: 22px;
            color: #333;
        }
        
        .chatbox {
            flex: 1;
            overflow-y: auto;
            padding: 20px;
            background-color: white;
            border-radius: 10px;
            box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
        }
        
        .message {
            margin-bottom: 15px;
            padding: 12px 15px;
            border-radius: 18px;
            max-width: 80%;
            line-height: 1.5;
            position: relative;
            animation: fadeIn 0.3s ease;
        }
        
        .user-message {
            background-color: #2575fc;
            color: white;
            margin-left: auto;
            border-bottom-right-radius: 5px;
        }
        
        .assistant-message {
            background-color: #f0f0f0;
            color: #333;
            margin-right: auto;
            border-bottom-left-radius: 5px;
        }
        
        .input-area {
            display: flex;
            gap: 10px;
        }
        
        #user-input {
            flex: 1;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 25px;
            outline: none;
            font-size: 16px;
            transition: border 0.3s;
        }
        
        #user-input:focus {
            border-color: #2575fc;
        }
        
        .send-btn {
            background: linear-gradient(90deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border: none;
            padding: 0 25px;
            border-radius: 25px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s;
        }
        
        .send-btn:hover {
            opacity: 0.9;
            transform: translateY(-2px);
        }
        
        .rating {
            display: flex;
            justify-content: center;
            margin-top: 10px;
            font-size: 18px;
        }
        
        .star {
            color: #ffc107;
            margin: 0 2px;
        }
        
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .container {
                flex-direction: column;
                height: auto;
            }
            
            .sidebar {
                width: 100%;
                height: auto;
            }
            
            .chat-container {
                height: 60vh;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h1>星座运势</h1>
                <p>点击星座查看今日运势</p>
            </div>
            <div class="zodiac-list">
                <div class="zodiac-item" onclick="queryHoroscope('白羊座')">白羊座</div>
                <div class="zodiac-item" onclick="queryHoroscope('金牛座')">金牛座</div>
                <div class="zodiac-item" onclick="queryHoroscope('双子座')">双子座</div>
                <div class="zodiac-item" onclick="queryHoroscope('巨蟹座')">巨蟹座</div>
                <div class="zodiac-item" onclick="queryHoroscope('狮子座')">狮子座</div>
                <div class="zodiac-item" onclick="queryHoroscope('处女座')">处女座</div>
                <div class="zodiac-item" onclick="queryHoroscope('天秤座')">天秤座</div>
                <div class="zodiac-item" onclick="queryHoroscope('天蝎座')">天蝎座</div>
                <div class="zodiac-item" onclick="queryHoroscope('射手座')">射手座</div>
                <div class="zodiac-item" onclick="queryHoroscope('摩羯座')">摩羯座</div>
                <div class="zodiac-item" onclick="queryHoroscope('水瓶座')">水瓶座</div>
                <div class="zodiac-item" onclick="queryHoroscope('双鱼座')">双鱼座</div>
            </div>
        </div>
        
        <div class="chat-container">
            <div class="chat-header">
                <h2 id="current-zodiac">星座运势</h2>
                <div class="rating" id="rating-stars">
                    <span class="star">★</span>
                    <span class="star">★</span>
                    <span class="star">★</span>
                    <span class="star">★</span>
                    <span class="star">★</span>
                </div>
            </div>
            
            <div class="chatbox" id="chatbox">
                <!-- 消息将在这里显示 -->
                <div class="message assistant-message">您好！我是星座运势助手，请点击左侧星座查看您的今日运势。</div>
            </div>
            
            <div class="input-area">
                <input type="text" id="user-input" placeholder="输入您的问题..." onkeypress="handleKeyPress(event)">
                <button class="send-btn" onclick="sendMessage()">发送</button>
            </div>
        </div>
    </div>

    <script>
        let messages = [
            { role: 'system', content: '你是一个星座运势专家，专门为用户提供星座运势分析。你的回答应该包含今日运势、幸运数字、幸运颜色和建议，并以星座符号开头。回答要简洁明了，不超过100字。' }
        ];

        // 星座符号映射
        const zodiacSymbols = {
            '白羊座': '♈',
            '金牛座': '♉',
            '双子座': '♊',
            '巨蟹座': '♋',
            '狮子座': '♌',
            '处女座': '♍',
            '天秤座': '♎',
            '天蝎座': '♏',
            '射手座': '♐',
            '摩羯座': '♑',
            '水瓶座': '♒',
            '双鱼座': '♓'
        };

        // 随机生成运势评级
        function generateRandomRating() {
            const stars = document.querySelectorAll('#rating-stars .star');
            const rating = Math.floor(Math.random() * 3) + 3; // 3-5星
            
            stars.forEach((star, index) => {
                if (index < rating) {
                    star.style.color = '#ffc107';
                } else {
                    star.style.color = '#ddd';
                }
            });
        }

        // 查询星座运势
        function queryHoroscope(zodiac) {
            document.getElementById('current-zodiac').textContent = `${zodiac} 今日运势`;
            generateRandomRating();
            
            const userInput = document.getElementById('user-input');
            userInput.value = `请告诉我${zodiac}今天的运势`;
            sendMessage();
        }

        // 发送消息
        async function sendMessage() {
            const userInput = document.getElementById('user-input');
            const chatbox = document.getElementById('chatbox');
            
            if (!userInput.value.trim()) return;

            // 添加用户消息
            messages.push({ role: 'user', content: userInput.value });
            chatbox.innerHTML += `<div class="message user-message">${userInput.value}</div>`;
            
            // 创建等待中的助理消息
            const assistantMsgDiv = document.createElement('div');
            assistantMsgDiv.className = 'message assistant-message';
            chatbox.appendChild(assistantMsgDiv);
            let finalMessages = null;

            try {
                const response = await fetch('http://localhost:8001/chat', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ messages })
                });

                const reader = response.body.getReader();
                const decoder = new TextDecoder();
                let assistantResponse = '';
                let jsonBuffer = '';

                while (true) {
                    const { done, value } = await reader.read();
                    if (done) break;

                    const chunk = decoder.decode(value);
                    jsonBuffer += chunk;

                    // 处理可能包含多个JSON对象的缓冲区
                    while (jsonBuffer.includes('\n\n')) {
                        const splitIndex = jsonBuffer.indexOf('\n\n');
                        const line = jsonBuffer.substring(0, splitIndex);
                        jsonBuffer = jsonBuffer.substring(splitIndex + 2);

                        if (line.startsWith('data: ')) {
                            try {
                                const event = JSON.parse(line.substring(6));
                                if (event.token) {
                                    assistantResponse += event.token;
                                    assistantMsgDiv.innerHTML = assistantResponse; 
                                }
                                // 新增消息历史处理
                                if (event.messages) {
                                    finalMessages = event.messages;
                                }
                            } catch (e) {
                                console.warn('JSON解析失败:', e);
                            }
                        }
                    }
                }

                // 修改为使用finalMessages
                messages = finalMessages || messages;
                userInput.value = '';
                chatbox.scrollTop = chatbox.scrollHeight;
            } catch (error) {
                console.error('请求错误:', error);
                assistantMsgDiv.innerHTML = '对话服务暂时不可用，请稍后再试。';
            }
        }

        // 按Enter键发送消息
        function handleKeyPress(event) {
            if (event.key === 'Enter') {
                sendMessage();
            }
        }
    </script>
</body>
</html>